<!--  -->
<template>
  <div class="msg-notify">
    <el-table size="medium" v-loading="loading" :data="dataMsgSet" style="width: 100%">
      <el-table-column prop="name" label="通知类型" :resizable="false" width="300">
      </el-table-column>
      <el-table-column label="邮箱通知" prop="statue" align="center" width="250">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isShow" active-color="#13ce66"
            @change="changeShow(scope.row.type, scope.row.isShow)">
          </el-switch>
          <el-button @click="edititem(scope.row)" type="text" size="small">设置</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="emailAddress" label="邮箱地址" :resizable="false">
      </el-table-column>
    </el-table>
    <DialogEmailSet ref="dialogEmailSet" :reSetData="reSetData"/>
  </div>
</template>
<script>
import DialogEmailSet from '@/pages/backend/user/host/messagenotify/msgnotify/DialogEmailSet.vue'
export default {
  created () {
    this.loading = true
    // 获取邮件提醒设置列表
    this.$post('/host/email-remind-config-list').then((response) => {
      if (response) {
        this.dataMsgSet = initdataMsgSet(response)
      }
      this.loading = false
    })
  },
  data () {
    return {
      loading: false,
      formLabelWidth: '120px',
      dataMsgSet: []
    }
  },
  methods: {
    changeShow (type, status) {
      let param = {
        type: type,
        status: status ? 1 : 0
      }
      // 请求后台修改状态
      this.$post('/host/update-email-remind-status', param).then(response => {
        if (response) {
          this.dataMsgSet = initdataMsgSet(response)
        }
      })
    },
    reSetData (data) {
      this.dataMsgSet = initdataMsgSet(data)
    },
    edititem (data) {
      this.$refs['dialogEmailSet'].show(data)
    }
  },
  components: {
    DialogEmailSet
  }
}
function initdataMsgSet (res) {
  let resData = JSON.parse(JSON.stringify(res))
  let initData = resData.map(item => {
    if (item.type === 'formEmail') {
      item.name = '表单留言通知'
    } else {
      item.name = item.type
    }
    item.isShow = item.status > 0
    return item
  })
  return initData
}
</script>

<style scoped lang="scss">
.msg-notify /deep/ .el-table th {
  color: #555;
}
.msg-notify /deep/ .el-table__fixed-right{
  height: 100% !important;
}
</style>
